package com.test.redis.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.test.redis.entity.MeetingRoom;
import com.test.redis.mapper.MeetingRoomMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 会议室服务 - 使用Spring Cache缓存会议室信息
 */
@Service
@Slf4j
public class MeetingRoomService {

    private final MeetingRoomMapper meetingRoomMapper;

    public MeetingRoomService(MeetingRoomMapper meetingRoomMapper) {
        this.meetingRoomMapper = meetingRoomMapper;
    }

    /**
     * 根据ID获取会议室信息 - 使用缓存
     */
    public MeetingRoom getById(Long id) {
        log.info("查询数据库获取会议室信息，id: {}", id);
        return meetingRoomMapper.selectById(id);
    }

    /**
     * 获取所有可用会议室 - 使用缓存
     */
    public List<MeetingRoom> getAllAvailableRooms() {
        log.info("查询数据库获取所有可用会议室");
        LambdaQueryWrapper<MeetingRoom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MeetingRoom::getStatus, 1)
                   .orderByAsc(MeetingRoom::getId);
        return meetingRoomMapper.selectList(queryWrapper);
    }

    /**
     * 更新会议室信息 - 更新后清除缓存
     */
    public void updateRoom(MeetingRoom room) {
        room.setUpdateTime(LocalDateTime.now());
        meetingRoomMapper.updateById(room);
        log.info("更新会议室信息，id: {}", room.getId());
    }

    /**
     * 新增会议室 - 清除所有会议室缓存
     */
    public void addRoom(MeetingRoom room) {
        room.setCreateTime(LocalDateTime.now());
        room.setUpdateTime(LocalDateTime.now());
        meetingRoomMapper.insert(room);
        log.info("新增会议室，id: {}", room.getId());
    }

    /**
     * 根据ID删除会议室 - 清除相关缓存
     */
    public void deleteRoom(Long id) {
        meetingRoomMapper.deleteById(id);
        log.info("删除会议室，id: {}", id);
    }
}